﻿
function ApartmentMap() {

    this.Canvas;
    this.ApartmentIcon;
    this.PlaceIcon;
    this.ApartmentPlace;
    this.ApartmentLocationMarker;
    this.Places = new Array();
    this.Map;

    this.SetCanvasMap = function (domElementId) {
        this.Canvas = document.getElementById(domElementId);
    }

    this.SetApartmentIcon = function (icon) {
        this.ApartmentIcon = icon;
    }

    this.SetPlaceIcon = function (icon) {
        this.PlaceIcon = icon;
    }

    this.SetApartment = function (apartmentName, apartmentAddress, lat, long) {
        this.ApartmentPlace = new PlaceLocation('Apartament', apartmentName, '', apartmentAddress, lat, long);
    }

    this.SetPlace = function (placeCategory, placeName, placeDescription, placeAddress, lat, long) {
        this.Places.push(new PlaceLocation(placeCategory, placeName, placeDescription, placeAddress, lat, long));
    }

    this.Populate = function () {
        var $apartmentMap = this;

        var $mapOptions = {
            center: $apartmentMap.ApartmentPlace.Location,
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        $apartmentMap.Map = new google.maps.Map($apartmentMap.Canvas, $mapOptions);
        //var $bounds = new google.maps.LatLngBounds();


        for (var xp = 0; xp < $apartmentMap.Places.length; xp++) {

            var $place = $apartmentMap.Places[xp];

            $place.Marker = new google.maps.Marker({
                position: $place.Location,
                map: $apartmentMap.Map,
                title: $place.PlaceName
            });

            if ($apartmentMap.PlaceIcon)
                $place.Marker.setIcon($apartmentMap.PlaceIcon);

            // dodanie popupa (infoWindow)
            var $cnt = '<div class="popupApartmentContent">' +
                            '<div class="pac-name"><span class="pac-n-cat">{category}</span> - {name}</div>' +
                            '<div class="pas-address">{address}</div>' +
                            '<div class="pas-description">{description}</div>' +
                            '</div>';

            $cnt = $cnt.replace('{category}', $place.PlaceCategory);
            $cnt = $cnt.replace('{name}', $place.PlaceName);
            $cnt = $cnt.replace('{address}', $place.PlaceAddress);
            $cnt = $cnt.replace('{description}', $place.PlaceDescription);

            $place.InfoWindow = new google.maps.InfoWindow({
                content: $cnt
            });

            var $marker = $place.Marker;
            $marker.info = $place.InfoWindow;
            $marker.line = new google.maps.Polyline({
                path: [$place.Location, $apartmentMap.ApartmentPlace.Location],
                strokeColor: '#c40000',
                strokeWeight: 1
            });
            $marker.ApartmentMap = $apartmentMap;

            google.maps.event.addListener($marker, 'click', function () {
                var am = this.ApartmentMap;
                for (var xp = 0; xp < $apartmentMap.Places.length; xp++) {
                    $apartmentMap.Places[xp].Marker.info.close();
                    $apartmentMap.Places[xp].Marker.line.setMap(null);
                }
                am.ApartmentLocationMarker.info.close();
                this.info.open($apartmentMap.Map, this);
                this.line.setMap($apartmentMap.Map);

            });

            //$bounds.extend($place.Location);
        }


        $apartmentMap.ApartmentLocationMarker = new google.maps.Marker({
            position: $apartmentMap.ApartmentPlace.Location,
            map: $apartmentMap.Map,
            title: 'Lokalizacja apartamentu'
        });
        if ($apartmentMap.ApartmentIcon)
            $apartmentMap.ApartmentLocationMarker.setIcon($apartmentMap.ApartmentIcon);

        var $cnt = '<div class="popupApartmentContent">' +
                            '<div class="pac-name"><span class="pac-n-cat">{category}</span> - {name}</div>' +
                            '<div class="pas-address">{address}</div>' +
                            '<div class="pas-description">{description}</div>' +
                            '</div>';

        $cnt = $cnt.replace('{category}', $apartmentMap.ApartmentPlace.PlaceCategory);
        $cnt = $cnt.replace('{name}', $apartmentMap.ApartmentPlace.PlaceName);
        $cnt = $cnt.replace('{address}', $apartmentMap.ApartmentPlace.PlaceAddress);
        $cnt = $cnt.replace('{description}', $apartmentMap.ApartmentPlace.PlaceDescription);

        $apartmentMap.ApartmentLocationMarker.info = new google.maps.InfoWindow({
            content: $cnt
        });

        $apartmentMap.ApartmentLocationMarker.ApartmentMap = $apartmentMap;

        google.maps.event.addListener($apartmentMap.ApartmentLocationMarker, 'click', function () {
            var am = this.ApartmentMap;
            for (var xp = 0; xp < $apartmentMap.Places.length; xp++) {
                $apartmentMap.Places[xp].Marker.info.close();
                $apartmentMap.Places[xp].Marker.line.setMap(null);
            }

            this.info.open($apartmentMap.Map, this);

        });

        //$bounds.extend($apartmentMap.ApartmentLocation);


        //$apartmentMap.Map.fitBounds($bounds);
        google.maps.event.addListenerOnce($apartmentMap.Map, 'bounds_changed', function () {
            $apartmentMap.ApartmentLocationMarker.info.open($apartmentMap.Map, $apartmentMap.ApartmentLocationMarker);
        });

    }

}

function PlaceLocation(placeCategory, placeName, placeDescription, placeAddress, lat, long) {
    this.PlaceCategory = placeCategory;
    this.PlaceDescription = placeDescription;
    this.PlaceName = placeName;
    this.PlaceAddress = placeAddress;
    this.Location = new google.maps.LatLng(lat, long);
}
